# -*- coding: utf-8 -*-

# 石油泄漏游戏

import sys, time, random, math, pygame
from pygame.locals import *
from MyLibrary import *
from datetime import datetime, date, time

reload(sys)
sys.setdefaultencoding('utf-8')

darktan = 190,190,110,255
tan = 210,210,130,255

class OilSprite(MySprite):
    def __init__(self):
        MySprite.__init__(self)
        self.radius = random.randint(0, 60) + 30
        play_sound(new_oil)

    def update(self, timing, rate=30):
        MySprite.update(self, timing, rate)

    def fade(self):
        r2 = self.radius//2
        color = self.image.get_at((r2,r2))
        if color.a > 5:
            color.a -= 5
            pygame.draw.circle(self.image, color, (r2, r2), r2, 0)
        else:
            oil_group.remove(self)
            play_sound(clean_oil)


def game_init():
    global screen, font, timer, backbuffer
    global oil_group, cursor, cursor_group
    global screen_width, screen_height

    screen_width = 800
    screen_height = 600

    pygame.init()
    winstyle = 0  # |FULLSCREEN
    bestdepth = pygame.display.mode_ok((screen_width, screen_height), winstyle, 32)
    screen = pygame.display.set_mode((screen_width, screen_height), winstyle, bestdepth)

    pygame.display.set_caption("石油泄漏游戏")
    font = pygame.font.Font("mini.ttf", 20)
    pygame.mouse.set_visible(False)
    timer = pygame.time.Clock()

    backbuffer = pygame.Surface((screen_width, screen_height))
    backbuffer.fill(darktan)
    oil_group = pygame.sprite.Group()

    # 鼠标图形
    cursor = MySprite()
    cursor.radius = 60
    image = pygame.Surface((60,60)).convert_alpha()
    image.fill((255,255,255,0))
    pygame.draw.circle(image, (80,80,220,70), (30,30), 30, 0)
    pygame.draw.circle(image, (80,80,250,255), (30,30), 30, 4)
    cursor.set_image(image)
    cursor_group = pygame.sprite.GroupSingle()
    cursor_group.add(cursor)


def audio_init():
    global new_oil, clean_oil

    pygame.mixer.init()
    new_oil = pygame.mixer.Sound("new_oil.WAV")
    clean_oil = pygame.mixer.Sound("clean_oil.WAV")


def play_sound(sound):
    channel = pygame.mixer.find_channel(True)
    channel.set_volume(0.5)
    channel.play(sound)


def add_oil():
    global oil_group, new_oil
    global screen_width, screen_height

    oil = OilSprite()
    image = pygame.Surface((oil.radius,oil.radius)).convert_alpha()
    image.fill((255,255,255,0))
    oil.fadelevel = random.randint(50, 150)
    oil_color = 10,10,20,oil.fadelevel
    r2 = oil.radius//2
    pygame.draw.circle(image, oil_color, (r2, r2), r2, 0)
    oil.set_image(image)
    oil.X = random.randint(0, screen_width-oil.radius)
    oil.Y = random.randint(0, screen_height-oil.radius)
    oil_group.add(oil)

game_init()
audio_init()
game_over = False
last_time = 0

while True:
    timer.tick(30)
    ticks = pygame.time.get_ticks()

    for event in pygame.event.get():
        if event.type == QUIT: sys.exit()

    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]: sys.exit()

    # 鼠标
    b1,b2,b3 = pygame.mouse.get_pressed()
    mx,my = pygame.mouse.get_pos()
    pos = (mx, my)
    if b1 > 0: pygame.draw.circle(backbuffer, tan, pos, 30, 0)

    oil_hit = None
    for oil in oil_group:
        if pygame.sprite.collide_circle_ratio(0.7)(cursor, oil):
            oil_hit = oil
            if b1 > 0: oil_hit.fade()
            break

    # 每隔一秒, 添加一个油点
    if ticks > last_time + 1000:
        add_oil()
        last_time = ticks

    screen.blit(backbuffer, (0,0))

    oil_group.update(ticks)
    oil_group.draw(screen)

    cursor.position = (mx-cursor.radius/2, my-cursor.radius/2)
    cursor_group.update(ticks)
    cursor_group.draw(screen)

    if oil_hit: print_text(font, 0, 0, u"请清洗油点")
    else: print_text(font, 0, 0, u"清洗")

    pygame.display.update()



